﻿using System;
using System.Collections;
using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// 错误集合
    /// </summary>
    [Serializable]
    public class ErrorResult : DataTable
    {
        public Hashtable IndexTable;
        //private Hashtable m_LevelSet=new Hashtable();

        /// <summary>
        /// 
        /// </summary>
        public ErrorResult()
        {
            this.Columns.Add(new DataColumn("行号", typeof (int)));
            this.Columns.Add(new DataColumn("出错信息", typeof (string)));
            IndexTable = new Hashtable();
        }

        /// <summary>
        /// 设定出错Message
        /// </summary>
        /// <param name="rowNumber">行数,用于表示这个错误信息来之于那行</param>
        /// <param name="message">错误信息</param>
        /// <exception cref="ArgumentNullException">message为空</exception>
        /// <exception cref="ArgumentOutOfRangeException">message长度为0或rowNumber少于0</exception>
        public void SetMessage(int rowNumber, string message)
        {
            if (message == null)
                throw new ArgumentNullException("message");
            if (message.Length == 0)
                throw new ArgumentOutOfRangeException("message", message.Length, "message长度不能为0");
            if (rowNumber < 0)
                throw new ArgumentOutOfRangeException("rowNumber", rowNumber, "rowNumber 不能少于0");
            DataRow row = GetRow(rowNumber);
            row[0] = rowNumber;
            row[1] += message + ";<br>";
        }
		
        /// <summary>
        /// 通过rowNumber获取数据源的rowIndex
        /// </summary>
        /// <param name="rowNumber">行数,错误信息的行数</param>
        /// <returns>如果找到，返回这个行数对应这个错误集合对应的行数，否则返回-1</returns>
        public int GetRowIndex(int rowNumber)
        {
            object o = this.IndexTable[rowNumber];
            if (o != null)
                return Convert.ToInt32(o);
            else
                return -1;
        }

        /// <summary>
        /// 检查制定的行数是否在这个错误信息集合中
        /// </summary>
        /// <param name="RowNumber"></param>
        /// <returns></returns>
        public bool InError(int RowNumber)
        {
            return this.IndexTable[RowNumber] != null;
        }

        /// <summary>
        /// 根据Index获取行对象
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DataRow GetRow(int index)
        {
            object o = IndexTable[index];
            if (o == null)
            {
                DataRow row = this.NewRow();
                IndexTable[index] = this.Rows.Count;
                this.Rows.Add(row);
                return row;

            }
            else
            {
                return this.Rows[Convert.ToInt32(o)];
            }
        }
    }
}
